package com.example.data;
import java.io.BufferedReader;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.entity.*;
import com.google.gson.stream.JsonReader;

public class JsonInfo {
	private String str="";
	public Weather weather = new Weather();
	
	public String getJson(int id){
		try {
			String s ="";
			URL url = new URL("http://wap.youhubst.com/weather/getweather.php?ID="+Integer.toString(id));
			InputStream inputstream = url.openStream();
			BufferedReader in = new BufferedReader(new InputStreamReader(inputstream,"UTF-8"));
	        while ((s = in.readLine()) != null) {
	            str += s;
	        }
	        in.close();
	        str=str.substring(15, str.length()-1);
	        str="[" + str;
	        str=str + "]";
           // System.out.println(str);
		}catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
	public Weather getWeather(String json){
		//json="[{\"city\":\"\u5f90\u5dde\",\"cityid\":\"101190801\",\"index\":\"\u8212\u9002\u5efa\u8bae\u7a7f\u957f\u8896\u886c\u886b\u5355\u88e4\u7b49\u670d\u88c5\u3002\",\"index_d\":\"\u8212\u9002\u5efa\u8bae\u7a7f\u957f\u8896\u886c\u886b\u5355\u88e4\u7b49\u670d\u88c5\u3002\",\"index_tr\":\"\u9002\u5b9c\u8f83\u5f31\u964d\u6c34\u548c\u5fae\u98ce\u5c06\u4f34\u60a8\u5171\u8d74\u65c5\u7a0b\u3002\",\"index_xc\":\"\u4e0d\u5b9c\u6709\u96e8\uff0c\u96e8\u6c34\u548c\u6ce5\u6c34\u4f1a\u5f04\u810f\u7231\u8f66\u3002\",\"index_uv\":\"\u6700\u5f31\u8f90\u5c04\u5f31\uff0c\u6d82\u64e6SPF8-12\u9632\u6652\u62a4\u80a4\u54c1\u3002\",\"index_co\":\"\u8212\u9002\u767d\u5929\u4e0d\u51b7\u4e0d\u70ed\uff0c\u98ce\u529b\u4e0d\u5927\u3002\",\"date_y\":\"2015\u5e7407\u670816\u65e5\",\"week\":\"\u661f\u671f\u56db\",\"temp1\":\"25\u00b0C~22\u00b0C\",\"weather1\":\"\u5c0f\u96e8\",\"wind1\":\"\u5fae\u98ce\",\"temp2\":\"27\u00b0C~21\u00b0C\",\"weather2\":\"\u5c0f\u96e8\",\"wind2\":\"\u5fae\u98ce\",\"temp3\":\"29\u00b0C~21\u00b0C\",\"weather3\":\"\u591a\u4e91\",\"wind3\":\"\u5fae\u98ce\",\"temp4\":\"30\u00b0C~23\u00b0C\",\"weather4\":\"\u591a\u4e91\",\"wind4\":\"\u5fae\u98ce\",\"temp5\":\"31\u00b0C~25\u00b0C\",\"weather5\":\"\u591a\u4e91\u8f6c\u9634\",\"wind5\":\"\u5fae\u98ce\",\"temp6\":\"28\u00b0C~22\u00b0C\",\"weather6\":\"\u5c0f\u96e8\",\"wind6\":\"\u5fae\u98ce\"}]";
		
		
		StringReader strreader = new StringReader(json);
		JsonReader jsonreader = new JsonReader(strreader);
		try{
			jsonreader.beginArray();
				while(jsonreader.hasNext()){
					jsonreader.beginObject();
					while(jsonreader.hasNext()){
						String name = jsonreader.nextName();
						if(name.equalsIgnoreCase("city")){
							String city = jsonreader.nextString();
							weather.setCity(city);
						}else if (name.equalsIgnoreCase("cityid")){
							String cityid = jsonreader.nextString();
							weather.setIndex_dress(cityid);
						}else if (name.equalsIgnoreCase("index")){
							String index = jsonreader.nextString();
							weather.setIndex_dress(index);
						}else if (name.equalsIgnoreCase("index_d")){
							String index_d = jsonreader.nextString();
							weather.setIndex_dress(index_d);
						}else if (name.equalsIgnoreCase("index_tr")){
							String index_tr = jsonreader.nextString();
							weather.setIndex_tr(index_tr);
						}else if (name.equalsIgnoreCase("index_xc")){
							String index_xc = jsonreader.nextString();
							weather.setIndex_xc(index_xc);
						}else if (name.equalsIgnoreCase("index_uv")){
							String index_uv = jsonreader.nextString();
							weather.setIndex_uv(index_uv);
						}else if (name.equalsIgnoreCase("index_co")){
							String index_co = jsonreader.nextString();
							weather.setIndex_co(index_co);
						}else if (name.equalsIgnoreCase("date_y")){
							String date = jsonreader.nextString();
							weather.setDate(date);
						}else if (name.equalsIgnoreCase("week")){
							String week = jsonreader.nextString();
							weather.setWeek(week);
						}else if (name.equalsIgnoreCase("temp1")){
							String temp1 = jsonreader.nextString();
							weather.setTemp1(temp1);
						}else if (name.equalsIgnoreCase("weather1")){
							String weather1 = jsonreader.nextString();
							weather.setWeather1(weather1);
						}else if (name.equalsIgnoreCase("wind1")){
							String wind1 = jsonreader.nextString();
							weather.setWind1(wind1);
						}else if (name.equalsIgnoreCase("temp2")){
							String temp2 = jsonreader.nextString();
							weather.setTemp2(temp2);
						}else if (name.equalsIgnoreCase("weather2")){
							String weather2 = jsonreader.nextString();
							weather.setWeather2(weather2);
						}else if (name.equalsIgnoreCase("wind2")){
							String wind2 = jsonreader.nextString();
							weather.setWind2(wind2);
						}else if (name.equalsIgnoreCase("temp3")){
							String temp3 = jsonreader.nextString();
							weather.setTemp3(temp3);
						}else if (name.equalsIgnoreCase("weather3")){
							String weather3 = jsonreader.nextString();
							weather.setWeather3(weather3);
						}else if (name.equalsIgnoreCase("wind3")){
							String wind3 = jsonreader.nextString();
							weather.setWind3(wind3);
						}else if (name.equalsIgnoreCase("temp4")){
							String temp4 = jsonreader.nextString();
							weather.setTemp4(temp4);
						}else if (name.equalsIgnoreCase("weather4")){
							String weather4 = jsonreader.nextString();
							weather.setWeather4(weather4);
						}else if (name.equalsIgnoreCase("wind4")){
							String wind4 = jsonreader.nextString();
							weather.setWind4(wind4);
						}else if (name.equalsIgnoreCase("temp5")){
							String temp5 = jsonreader.nextString();
							weather.setTemp5(temp5);
						}else if (name.equalsIgnoreCase("weather5")){
							String weather5 = jsonreader.nextString();
							weather.setWeather5(weather5);
						}else if (name.equalsIgnoreCase("wind5")){
							String wind5 = jsonreader.nextString();
							weather.setWind5(wind5);
						}else if (name.equalsIgnoreCase("temp6")){
							String temp6 = jsonreader.nextString();
							weather.setTemp6(temp6);
						}else if (name.equalsIgnoreCase("weather6")){
							String weather6 = jsonreader.nextString();
							weather.setWeather6(weather6);
						}else if (name.equalsIgnoreCase("wind6")){
							String wind6 = jsonreader.nextString();
							weather.setWind6(wind6);
							System.out.println(wind6);
						}
						
					}jsonreader.endObject();
				}
			}catch (IOException e){
				e.printStackTrace();
			}	
		
		return weather;
	}
	
}
